package com.wolfheader.wolfgame.entity.admin;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.wolfheader.wolfgame.entity.BaseEntity;

@Entity
@Table(name = "t_admin")
public class AdminEntity extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8243564999240970015L;

	/** “密钥”参数 */
	public static final String KEY_ATTR = "adminKey";

	private String username;

	private String password;

	private String phone;

	private String realName;

	private String wxName;

	private String idNo;

	private Boolean enabled;

	private String loginIp;

	private Date loginDate;

	private Set<RoleEntity> roles = new HashSet<RoleEntity>();

	private Date memberDate;

	public String getIdNo() {
		return idNo;
	}

	public void setIdNo(String idNo) {
		this.idNo = idNo;
	}

	public Date getMemberDate() {
		return memberDate;
	}

	public void setMemberDate(Date memberDate) {
		this.memberDate = memberDate;
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	@Column(nullable = false)
	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

    @Column(nullable = false, updatable = false, unique = true)
	public String getUsername() {
		return username;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_admins_roles")
	public Set<RoleEntity> getRoles() {
		return roles;
	}

	public void setRoles(Set<RoleEntity> roles) {
		this.roles = roles;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@JsonProperty("enabled")
	@Transient
	public String getEnabledAsJson() {
		return enabled ? "是" : "否";
	}

	@JsonProperty("loginIp")
	@Transient
	public String getLoginIpAsJson() {
		return loginIp;
	}

	@JsonProperty("phone")
	@Transient
	public String getPhoneAsJson() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(length = 50)
	public String getRealName() {
		return realName;
	}

	@JsonProperty("realname")
	@Transient
	public String getRealNameAsJson() {
		return realName;
	}

	@JsonProperty("wxname")
	@Transient
	public String getWxNameAsJson() {
		return wxName;
	}

	public void setWxName(String wxName) {
		this.wxName = wxName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getWxName() {
		return wxName;
	}

	@Column(unique = true, length = 20)
	public String getPhone() {
		return phone;
	}

	@JsonProperty("name")
	@Transient
	public String getUsernameAsJson() {
		return username;
	}

	@Transient
	public Set<String> getRolesName() {
		Set<RoleEntity> roles = getRoles();
		Set<String> set = new HashSet<String>();
		for (RoleEntity role : roles) {
			set.add(role.getRolename());
		}
		return set;
	}

	@PrePersist
	public void prePersist() {
		if (getEnabled() == null) {
			setEnabled(false);
		}
	}
	
	/**
	 * 验证登录密码
	 * 
	 * @param password
	 *            密码
	 * @return 验证是否通过
	 */
	@Transient
	public boolean verifyLoginPassword(String password) {
		return StringUtils.equals(DigestUtils.md5Hex(password), getPassword());
	}

	/**
	 * 设置新密码
	 * 
	 * @param password
	 *            密码
	 */
	@Transient
	public void setNewPassword(String password) {
		setPassword(DigestUtils.md5Hex(password));
	}
}
